<template>
  <div class="home">
    <div class="title-container">
      <div class="search-content">
        <el-form :model="searchModel" :inline="true">
          <el-form-item>
            <el-input
              v-model="searchModel.userName"
              :disabled="detailsDisabled"
              placeholder="请输入自定义名称"
              prefix-icon="el-icon-search"
            />
          </el-form-item>
        </el-form>
        <div class="search-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchClick"
            size="medium"
          >搜索
          </el-button>
        </div>
      </div>
    </div>

    <el-card>
      <div class="addAccount">
        <perButton label="新建" type="primary" icon="el-icon-plus" size="medium" perms="PT_USER_ADD"
                   @click="handlerAccount()"></perButton>
        <el-button
          type="success"
          icon="el-icon-refresh"
          size="medium"
          @click="refresh"
        >刷新
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        row-key="id"
        border
        style="width: 100%;"
        class="elTable"
      >
        <el-table-column type="index" label="序号" width="50" align="center"/>
        <!--        <el-table-column prop="cusParameterCode" label="自定义编码" align="center"/>-->
        <el-table-column prop="cusParameterName" label="自定义名称" align="center"/>
        <el-table-column prop="cusParameterType" label="数据类型" align="center">
          <template slot-scope="scope">
            {{ scope.row.cusParameterType=="number"?"数值形":"布尔形" }}
          </template>
        </el-table-column>
        <el-table-column prop="cusParameterDes" label="自定义描述" align="center" show-overflow-tooltip/>
        <el-table-column prop="initData" label="初始化值" align="center">
          <template slot-scope="scope">
            {{ scope.row.cusParameterType=="number"?scope.row.initData:scope.row.initData=="0"?"false":"true" }}
          </template>
        </el-table-column>
        <el-table-column prop="falseZero" label="布尔0值描述" align="center"/>
        <el-table-column prop="trueOne" label="布尔1值描述" align="center"/>
        <el-table-column label="是否定时重置" align="center">
          <template slot-scope="scope">
            {{ scope.row.canTimeInit=="Y"?"是":"否" }}
          </template>
        </el-table-column>
        <el-table-column prop="timeInit" label="重置时间" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.canTimeInit=='Y'">
              {{ scope.row.timeInit=="Day"?"跨天":scope.row.timeInit=="Month"?"跨月":"跨年" }}
            </div>
            <div v-else>{{scope.row.timeInit}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="timeInitDate" label="重置值" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.canTimeInit=='Y'">
              {{ scope.row.cusParameterType=="number"?scope.row.initData:scope.row.timeInitDate=="0"?"false":"true" }}
            </div>
            <div v-else>{{scope.row.timeInitDate}}</div>
          </template>
        </el-table-column>
        <!--        <el-table-column prop="createByName" label="创建人" align="center"/>-->
        <!--        <el-table-column label="创建时间" align="center" prop="createTime">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.createTime }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column prop="updateByName" label="更新人" align="center"/>-->
        <!--        <el-table-column label="更新时间" align="center" prop="createTime">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.updateTime }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作" width="180" align="center">
          <template #default="{ row }">
            <perButton label="编辑" type="warning" size="mini" perms="PT_USER_EDIT"
                       @click="handlerEdit(row)"></perButton>
            <perButton label="删除" type="danger" size="mini" perms="PT_USER_DEL"
                       @click="handlerDelete(row)"></perButton>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        class="pagination"
        :total="count"
        :page.sync="pageIndex"
        :limit.sync="pageSize"
        @pagination="getAccountData"
      />
    </el-card>

    <!-- 新建 -->
    <yc-dialog ref="ycDialog" :dialogTitle="dialogTitle" :show-center="false" dialog-width="40%">
      <template #content>
        <div class="dialog-content">
          <el-form
            ref="newAccountFormRef"
            :rules="newAccountModelRules"
            :model="newAccountModel"
            :disabled="detailsDisabled"
            label-width="130px"
          >
            <el-form-item label="自定义数据名称" prop="cusParameterName">
              <el-input v-model="newAccountModel.cusParameterName"/>
            </el-form-item>
            <el-form-item label="自定义数据类型" prop="cusParameterType">
              <el-select v-model="newAccountModel.cusParameterType" placeholder="请选择" style="width: 100%;">
                <el-option label="数值形" value="number"></el-option>
                <el-option label="布尔形" value="bo"></el-option>
              </el-select>
            </el-form-item>
            <div v-if="newAccountModel.cusParameterType=='bo'">
              <el-form-item label="自定义初始化值" prop="initData">
                <el-select v-model="newAccountModel.initData" placeholder="请选择" style="width: 100%;">
                  <el-option label="true" value="1"></el-option>
                  <el-option label="false" value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="布尔0值描述" prop="falseZero">
                <el-input v-model="newAccountModel.falseZero"/>
              </el-form-item>
              <el-form-item label="布尔1值描述" prop="trueOne">
                <el-input v-model="newAccountModel.trueOne"/>
              </el-form-item>
            </div>
            <div v-else>
              <el-form-item label="自定义初始化值" prop="initData">
                <el-input v-model="newAccountModel.initData"/>
              </el-form-item>
            </div>
            <el-form-item label="是否定时重置" prop="canTimeInit">
              <el-select v-model="newAccountModel.canTimeInit" placeholder="请选择" style="width: 100%;">
                <el-option label="是" value="Y"></el-option>
                <el-option label="否" value="N"></el-option>
              </el-select>
            </el-form-item>
            <div v-if="newAccountModel.canTimeInit=='Y'">
              <el-form-item label="重置周期" prop="timeInit">
                <el-select v-model="newAccountModel.timeInit" placeholder="请选择" style="width: 100%;">
                  <el-option label="跨天" value="Day"></el-option>
                  <el-option label="跨月" value="Month"></el-option>
                  <el-option label="跨年" value="Year"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="重置值" prop="timeInitDate">
                <el-input v-if="newAccountModel.cusParameterType=='number'" v-model="newAccountModel.timeInitDate"/>
                <el-select v-else v-model="newAccountModel.timeInitDate" placeholder="请选择" style="width: 100%;">
                  <el-option label="true" value="1"></el-option>
                  <el-option label="false" value="0"></el-option>
                </el-select>
              </el-form-item>
            </div>
            <el-form-item label="自定义数据描述" prop="cusParameterDes">
              <el-input v-model="newAccountModel.cusParameterDes"/>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="addOrUpdateLoading" @click="dialogSave">确定</el-button>
        <el-button @click="$refs.ycDialog.dialogVisible = false">取消</el-button>
      </template>
    </yc-dialog>

    <!-- 封装操作后弹框 -->
    <global-message
      v-if="isGlobalMessageShow"
      @receive="receive"
      :message="message"
    />
  </div>
</template>

<script>
  import ycDialog from "@/components/pageDialog"
  import perButton from "@/components/permissionButton"

  import {
    queryPage,
    deleteByModel,
    insertByModel,
    updateByModel
  } from "@/api/dataManage/customize"

  export default {
    components: {
      ycDialog,
      perButton
    },
    data() {
      return {
        isGlobalMessageShow: false,
        message: "",

        tableData: [],
        dataItemObj: {},
        count: 0,
        pageIndex: 1,
        pageSize: 10,
        loading: false,

        searchModel: {
          userName: "",
          status: null //正常/停用状态（全部）
        },

        newAccountModel: {
          cusParameterName: "",
          cusParameterType: "",
          falseZero: "",
          trueOne: "",
          cusParameterDes: "",
          initData: "",
          canTimeInit: "",
          timeInit: "",
          timeInitDate: "",
          id: "0"
        },
        newAccountModelRules: {
          cusParameterName: [{ required: true, message: "请输入自定义数据名称", trigger: "blur" }],
          initData: [{ required: true, message: "请输入自定义初始化值", trigger: "blur" }],
          falseZero: [{ required: true, message: "请输入布尔0值描述", trigger: "blur" }],
          trueOne: [{ required: true, message: "请输入布尔1值描述", trigger: "blur" }],
          cusParameterType: [{ required: true, message: "请选择自定义数据类型", trigger: "change" }],
          canTimeInit: [{ required: true, message: "请选择是否定时重置", trigger: "change" }],
          timeInit: [{ required: true, message: "请选择重置周期", trigger: "change" }],
          timeInitDate: [{ required: true, message: "请输入重置值", trigger: "blur" }]
        },

        deviceTypeList: [],
        tagList: [],
        tagProps: {
          label: "tagName",
          value: "tagCode",
          children: "childList",
          checkStrictly: true
        },

        dialogTitle: "",
        centerTitle: "",

        detailsDisabled: false,
        addOrUpdateLoading: false
      }
    },
    created() {
      this.getAccountData()
    },
    methods: {
      /**
       * 获取表格方法
       */
      async getAccountData(tableInfo) {
        this.loading = true
        if (tableInfo) {
          const { userName, status } = tableInfo
          const { object } = await queryPage({
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            userName,
            status
          })
          this.tableData = object.data
          this.count = object.count
          this.loading = false
        } else {
          const { object } = await queryPage({
            pageIndex: this.pageIndex,
            pageSize: this.pageSize
          })
          this.tableData = object.data
          this.count = object.count
          this.loading = false
        }
      },
      pagination(pageItem) {
        this.getAccountData(pageItem.page, pageItem.limit)
      },
      // 搜索按钮
      async searchClick() {
        this.getAccountData(this.searchModel)
      },

      /**
       * 删除按钮
       */
      async handlerDelete({ id }) {
        this.$confirm("确定删除此数据吗", "删除操作", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error",
          center: true
        }).then(async () => {
          const { message, status } = await deleteByModel({ id })
          if (status == "200") {
            this.getAccountData()
            this.isGlobalMessageShow = true
            this.message = "success"
          } else {
            this.message = "error"
          }
        })
      },

      /**
       * 新建
       * */
      async handlerAccount(row) {
        this.dialogTitle = "添加"
        this.centerTitle = "添加"

        this.detailsDisabled = false
        this.addOrUpdateLoading = false
        this.$refs.ycDialog.dialogVisible = true

        Object.keys(this.newAccountModel).forEach((item) => {
          this.newAccountModel[item] = ""
        })
      },

      /**
       * 修改
       */
      handlerEdit(row) {
        this.dialogTitle = "编辑"
        this.centerTitle = "编辑"

        this.newAccountModel.id = row.id
        this.newAccountModel.cusParameterName = row.cusParameterName
        this.newAccountModel.cusParameterType = row.cusParameterType
        this.newAccountModel.initData = row.initData
        this.newAccountModel.falseZero = row.falseZero
        this.newAccountModel.trueOne = row.trueOne
        this.newAccountModel.canTimeInit = row.canTimeInit
        this.newAccountModel.timeInit = row.timeInit
        this.newAccountModel.timeInitDate = row.timeInitDate
        this.newAccountModel.cusParameterDes = row.cusParameterDes

        this.addOrUpdateLoading = false
        this.detailsDisabled = false
        this.$refs.ycDialog.dialogVisible = true
      },

      dialogSave() {
        this.$refs.newAccountFormRef.validate(async (v) => {
          if (v) {
            this.addOrUpdateLoading = true
            if (this.detailsDisabled) {
              // 详情的确定
              this.$refs.ycDialog.dialogVisible = false
            } else if (this.newAccountModel.id) {
              const { message, status } = await updateByModel({
                ...this.newAccountModel
              })
              if (status == 200) {
                this.getAccountData()
                this.$refs.ycDialog.dialogVisible = false
                this.$message.success(message)
              }
            } else {
              const { message, status } = await insertByModel({
                ...this.newAccountModel
              })
              if (status == 200) {
                this.getAccountData()
                this.$refs.ycDialog.dialogVisible = false
                this.$message.success(message)
              }
            }
            this.addOrUpdateLoading = false
          }
        })
      },
      /**
       * 刷新
       */
      refresh() {
        this.searchModel = {
          userName: "",
          status: null
        }
        this.getAccountData()
      },

      /**
       * globalMessage的知道按钮的方法
       * */
      receive() {
        this.isGlobalMessageShow = false
      }
    }
  }
</script>

<style lang="scss" scoped>
  .status {
    display: inline-block;
    padding: 0 10px;
  }

  .title-container {
    display: flex;
    justify-content: flex-end;
    height: 60px;

    .search-content {
      margin-left: 20px;
      display: flex;
    }

    .addAccount {
      margin: 10px 20px;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
  }


  .footer {
    height: 360px;
    display: flex;
    align-items: flex-end;
    // justify-content: st-end;
    padding: 5px 20px;
  }
</style>
